
import java.awt.*;
import java.awt.image.*;

public class Tile extends GameObject {

	final int 						numImages 	  = 37;		// Total number of tiles
	final int 						tileHeight    = 16;
	final int 						tileWidth     = 16;
	final String 					imageName = "/images/tiles.png";
	public static	BufferedImage 	tileImages[];
	public static	CollisionRect 	tileRects[];
	public static	Boolean     	initialized = false;
	
	
	TileType            			tileType = TileType.BASEROCK;

	// Tile  Row 0
	// 0     BASEROCK 	- 0
	// 1     BRICK    	- 1
	// 2    ? brick  	- 2 - Should be sprite
	// 3    Castle top 	- 3
	//       Row 1 Mountain
	// 4-8   Mountain Top - 0-5
	//       Row 2 
	// 9-13  Mountain Mid - 0-5
	//       Row 3 
	// 14-18 Mountain bottom - 0-5
	//       Row 4 
	// 19    Sky 3 
	//       Row 5 
	// 20,21 Pipe top 0,1
	//       Row 6 
	// 22,23 Pipe bottom 0,1
	//       Row 7 
	// 24,25 cloud top 0,1
	//       Row 8 
	// 26,27 cloud bottom 0,1
	//       Row 9 
	// 28,29,30 bush 0-2
	//       Row 10 
	// 31,32 flag top 0,1
	//       Row 11 
	// 33,34 flag middle 0,1
	//       Row 12 
	// 35,36 flag bottom 0,1

	// Tile Contructor
	Tile(){
		type = ObjectType.BACKGROUND;
		if( !initialized ){
            initialized = true; 
			// Load Background image set
			ImageLoader backgroundSet = new ImageLoader();
			backgroundSet.LoadImage(imageName);
			tileImages 	= new BufferedImage[numImages];
			tileRects   = new CollisionRect[numImages];
		
			type = ObjectType.BACKGROUND;
		
			// Row 0
			InitTile(TileType.BASEROCK, 	backgroundSet.image, 0 , 0, true);
			InitTile(TileType.BRICK, 		backgroundSet.image, 1 , 0, true);
			InitTile(TileType.BRICK_QUEST, 	backgroundSet.image, 2 , 0, true);
			InitTile(TileType.CASTLE_TOP, 	backgroundSet.image, 3 , 0, true);
			// Row 1                                      //Row 1 Mount
			InitTile(TileType.MOUNTAIN_TOP0, backgroundSet.image, 0 , 1, false) ;
			InitTile(TileType.MOUNTAIN_TOP1, backgroundSet.image, 1 , 1, false) ;
			InitTile(TileType.MOUNTAIN_TOP2, backgroundSet.image, 2 , 1, false) ;
			InitTile(TileType.MOUNTAIN_TOP3, backgroundSet.image, 3 , 1, false) ;
			InitTile(TileType.MOUNTAIN_TOP4, backgroundSet.image, 4 , 1, false) ;
			// Row 2                      
			InitTile(TileType.MOUNTAIN_MID0, backgroundSet.image, 0 , 2, false );
			InitTile(TileType.MOUNTAIN_MID1, backgroundSet.image, 1 , 2, false );
			InitTile(TileType.MOUNTAIN_MID2, backgroundSet.image, 2 , 2, false );
			InitTile(TileType.MOUNTAIN_MID3, backgroundSet.image, 3 , 2, false );
			InitTile(TileType.MOUNTAIN_MID4, backgroundSet.image, 4 , 2, false );
			// Row 3                      
			InitTile(TileType.MOUNTAIN_BOT0, backgroundSet.image, 0 , 3, false );
			InitTile(TileType.MOUNTAIN_BOT1, backgroundSet.image, 1 , 3, false );
			InitTile(TileType.MOUNTAIN_BOT2, backgroundSet.image, 2 , 3, false );
			InitTile(TileType.MOUNTAIN_BOT3, backgroundSet.image, 3 , 3, false );
			InitTile(TileType.MOUNTAIN_BOT4, backgroundSet.image, 4 , 3, false );
			// Row 4                   
			InitTile(TileType.SKY, backgroundSet.image, 0 , 4, false  			  );
			// Row 5                                       // Row 5 
			InitTile(TileType.PIPE_TOP0, backgroundSet.image, 0 , 5, true) 		;
			InitTile(TileType.PIPE_TOP1, backgroundSet.image, 1 , 5, true) 		;
			// Row 6                                       // Row 6 
			InitTile(TileType.PIPE_BOT0, backgroundSet.image, 0 , 6, true) 		;
			InitTile(TileType.PIPE_BOT1, backgroundSet.image, 1 , 6, true) 		;
			// Row 7                                       // Row 7 
			InitTile(TileType.CLOUD_TOP0, backgroundSet.image, 0 , 7, false   );
			InitTile(TileType.CLOUD_TOP1, backgroundSet.image, 1 , 7, false   );
			// Row 8                                       // Row 8 
			InitTile(TileType.CLOUD_BOT0, backgroundSet.image, 0 , 8, false   );
			InitTile(TileType.CLOUD_BOT1, backgroundSet.image, 1 , 8, false   );
			// Row 9                                       // Row 9 
			InitTile(TileType.BUSH_0, backgroundSet.image, 0 , 9, false       );
			InitTile(TileType.BUSH_1, backgroundSet.image, 1 , 9, false       );
			InitTile(TileType.BUSH_2, backgroundSet.image, 2 , 9, false       );
			// Row 10                                      // Row 10 
			InitTile(TileType.FLAG_TOP0, backgroundSet.image, 0 , 10, false);
			InitTile(TileType.FLAG_TOP1, backgroundSet.image, 1 , 10, false);
			// Row 11                                      // Row 11 
			InitTile(TileType.FLAG_MID0, backgroundSet.image, 0 , 11, false);
			InitTile(TileType.FLAG_MID1, backgroundSet.image, 1 , 11, false);
			// Row 12                                      // Row 12 
			InitTile(TileType.FLAG_BOT0, backgroundSet.image, 0 , 12, false);
			InitTile(TileType.FLAG_BOT1, backgroundSet.image, 1 , 12, false);
		}
	}
	
	void DrawImage(Graphics imageGraphics, int xOffset, int yOffset){
		int x = xPos - xOffset;
		int y = yPos - yOffset;
        imageGraphics.drawImage(tileImages[ tileType.TileType ] , x  , y , x + tileWidth ,y + tileHeight , 0, 0, tileWidth, tileHeight , null);
	}
	
	void InitTile(TileType type, BufferedImage backgroundSet, int column, int row, boolean solidRect ){
		// InitTile - Initialize image
		int x = column * tileWidth;
		int y = row *    tileHeight;
		int imageNumber = type.TileInt();

		tileImages[ imageNumber] = backgroundSet.getSubimage(x , y, tileWidth, tileHeight);
		
        tileRects[imageNumber] = new CollisionRect();
        tileRects[imageNumber].SetDimensions(solidRect , tileWidth, tileHeight);
	}
	
	void SetTileType( TileType type){
		tileType = type;
		colRect  = new CollisionRect();
		colRect.CopyDimensions(tileRects[type.TileType]);
	}
	void SetPosition (int x, int y){
		this.xPos = x;
		this.yPos = y;
		colRect.SetPosition(x, y);
	}
	
	int GetTileWidth(){
		return tileWidth;     
	}
	
	int GetTileHeight(){
		return tileHeight;     
	}
	
}

